package com.livepan.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.livepan.server.dao.TeacherDOMapper;
import com.livepan.server.dataobject.TeacherDO;
import com.livepan.server.dataobject.TeacherDOExample;
import com.livepan.server.error.BusinessException;
import com.livepan.server.error.EmBusinessError;
import com.livepan.server.service.model.TeacherModel;
import com.livepan.server.service.model.PageModel;
import com.livepan.server.utils.CopyUtil;
import com.livepan.server.utils.UUIDUtil;
import com.livepan.server.utils.ValidatorUtil;
import com.livepan.server.validator.ValidationResult;
import com.livepan.server.validator.ValidatorImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.nio.cs.CharsetMapping;


import java.util.List;
import java.util.stream.Collectors;

@Service
public class TeacherService {
    @Autowired
    private TeacherDOMapper teacherDOMapper;

    @Autowired
    private ValidatorImpl validator;

    public void list(PageModel pageModel) {
        PageHelper.startPage(pageModel.getPage(), pageModel.getSize());
        TeacherDOExample teacherDOExample = new TeacherDOExample();
        List<TeacherDO> teacherDOList = teacherDOMapper.selectByExample(teacherDOExample);
        PageInfo<TeacherDO> pageInfo = new PageInfo<>(teacherDOList);
        pageModel.setTotal(pageInfo.getTotal());
        List<TeacherModel> teacherModelList = teacherDOList.stream().map(
                this::convertTeacherModelFromTeacherDO
        ).collect(Collectors.toList());
        pageModel.setList(teacherModelList);
    }

    public List<TeacherModel> all() {
        TeacherDOExample teacherDOExample = new TeacherDOExample();
        List<TeacherDO> teacherDOList = teacherDOMapper.selectByExample(teacherDOExample);
        List<TeacherModel> teacherModelList = teacherDOList.stream().map(
                this::convertTeacherModelFromTeacherDO
        ).collect(Collectors.toList());
        return teacherModelList;
    }

    public void save(TeacherModel teacherModel) throws BusinessException {
        ValidationResult validateResult = validator.validate(teacherModel);
        if (validateResult.isHasError()) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, validateResult.getErrMsg());
        }
        TeacherDO teacherDO = CopyUtil.copy(teacherModel, TeacherDO.class);
        if (teacherDOMapper.selectByPrimaryKey(teacherDO.getId()) == null) {
            this.insert(teacherDO);
        } else {
            this.update(teacherDO);
        }
    }

    private void insert(TeacherDO teacherDO) {
        teacherDO.setId(UUIDUtil.getShortUUID());
        teacherDOMapper.insert(teacherDO);
    }

    private void update(TeacherDO teacherDO) {
        teacherDOMapper.updateByPrimaryKey(teacherDO);
    }

    public void delete(String id) {
        teacherDOMapper.deleteByPrimaryKey(id);
    }


    private TeacherModel convertTeacherModelFromTeacherDO(TeacherDO teacherDO) {
        if (teacherDO == null) {
            return null;
        }
        TeacherModel teacherModel = new TeacherModel();
        BeanUtils.copyProperties(teacherDO, teacherModel);
        return teacherModel;
    }
}
